package cn.wuyu.baselibrary.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.KeyEvent;

import androidx.appcompat.app.AlertDialog;
import cn.wuyu.baselibrary.R;
import cn.wuyu.baselibrary.utils.ResUtils;

/**
 * author : xuzongmeng
 * date   : 2019/1/15.
 * desc   : 描述对TDialog再封装
 */
public class BaseDialog {
    /************** 通用弹窗 Builder************/
//	@SuppressWarnings("deprecation")
//	public static class confirmBuilder {
//		private FragmentManager fragmentManager;
//		private View.OnClickListener confirmListener;
//		private View.OnClickListener cancelListener;
//		private String title;
//		private String hintMsg;
//		private String confirmText;
//		private String cancelText;
//		private boolean isConfirmVisible = true;
//		private boolean isCancelVisible = true;
//		private boolean isTitleVisible = true;
////		private TDialog tDialog;
//		private boolean isInputMode=false;
//		private void initDialog() {
//			//通过BindViewHolder拿到控件对象,进行修改
//			new TDialog.Builder(fragmentManager)
//					//设置弹窗展示的xml布局
//					.setLayoutRes(R.layout.dialog_custom_layout)
//					.setScreenWidthAspect(AppUtil.getAppContext(), 0.8f)
////                  .setScreenHeightAspect(this, 1f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
//					.setGravity(Gravity.CENTER)     //设置弹窗展示位置
//					.setTag("dialog")   //设置Tag
//					.setDimAmount(0.4f)     //设置弹窗背景透明度(0-1f)
//					.setCancelableOutside(false)     //弹窗在界面外是否可以点击取消
////                  .setDialogAnimationRes(R.style.animate_dialog)  //设置弹窗动画
//					.setOnDismissListener(dialog -> {
//					})
//					.setOnBindViewListener(holder -> {
//						TextView tvTitle = holder.getView(R.id.tvTitle);
//						TextView tvHintMsg = holder.getView(R.id.tvHintMsg);
//						TextView tvConfirm = holder.getView(R.id.tvConfirm);
//						TextView tvCancel = holder.getView(R.id.tvCancel);
//						View viewLine = holder.getView(R.id.viewLine);
////						EditText etInput = holder.getView(R.id.etInput);
//                        if (!isTitleVisible){
//                        	tvTitle.setVisibility(View.GONE);
//						}
//						if (!isConfirmVisible){
//							tvConfirm.setVisibility(View.GONE);
//							viewLine.setVisibility(View.GONE);
//						}
//						if (!isCancelVisible){
//							tvCancel.setVisibility(View.GONE);
//							viewLine.setVisibility(View.GONE);
//						}
//						if (!TextUtils.isEmpty(confirmText)) {
//							tvConfirm.setText(title);
//						}
//						if (!TextUtils.isEmpty(cancelText)) {
//							tvCancel.setText(title);
//						}
//
//						if (isInputMode){
////							etInput.setVisibility(View.VISIBLE);
//							tvHintMsg.setVisibility(View.GONE);
//						}
//						if (!TextUtils.isEmpty(title)) {
//							tvTitle.setText(title);
//						} else {
//							tvTitle.setText("提示");
//						}
//
//						if (!TextUtils.isEmpty(hintMsg)) {
//							if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
//								tvHintMsg.setText(Html.fromHtml(hintMsg,Html.FROM_HTML_MODE_LEGACY));
//							} else {
//								tvHintMsg.setText(Html.fromHtml(hintMsg));
//							}
//						}
//					})
//					.addOnClickListener(R.id.tvCancel, R.id.tvConfirm)   //添加进行点击控件的id
//					.setOnViewClickListener((viewHolder, view, dialog) -> {
//						int i = view.getId();
//						if (i == R.id.tvCancel) {
//							if (cancelListener != null) {
//								cancelListener.onClick(view);
//							}
//							dialog.dismiss();
//						} else if (i == R.id.tvConfirm) {
//							if (confirmListener != null) {
//								confirmListener.onClick(view);
//							}
//							dialog.dismiss();
//						}
//					})
//					.create()  //创建TDialog
//					.show();    //展示
//		}
//
//		public confirmBuilder(FragmentManager fragmentManager) {
//			this.fragmentManager = fragmentManager;
//		}
//
////		public void show(String content) {
////			this.hintMsg = content;
////			initDialog();
////		}
//		public void show() {
//			initDialog();
//		}
//		public confirmBuilder setHintMsg(String hintMsg) {
//			this.hintMsg = hintMsg;
//			return this;
//		}
////		public confirmBuilder setHintMsg(int hintMsg) {
////			this.hintMsg = hintMsg;
////			return this;
////		}
//		public confirmBuilder setHintMsg(int resId) {
//			this.hintMsg = ResUtils.getString(resId);
//			return this;
//		}
//		public confirmBuilder setTitle(String title) {
//			this.title = title;
//			return this;
//		}
//		public confirmBuilder setTitleVisible(boolean isTitleVisible) {
//			this.isTitleVisible = isTitleVisible;
//			return this;
//		}
//		public confirmBuilder setConfirmClickListener(View.OnClickListener confirmListener) {
//			this.confirmListener = confirmListener;
//			return this;
//		}
//
//		public confirmBuilder setCancelListener(View.OnClickListener cancelListener) {
//			this.cancelListener = cancelListener;
//			return this;
//		}
//
//		public confirmBuilder setConfirmVisible(boolean confirmVisible) {
//			this.isConfirmVisible = confirmVisible;
//			return this;
//		}
//
//		public confirmBuilder setCancelVisible(boolean cancelVisible) {
//			this.isCancelVisible = cancelVisible;
//			return this;
//		}
//
//		public confirmBuilder setConfirmText(String confirmText) {
//			this.confirmText = confirmText;
//			return this;
//		}
//
//		public confirmBuilder setCanceText(String cancelText) {
//			this.cancelText = cancelText;
//			return this;
//		}
//		public confirmBuilder setInputMode() {
//			this.isInputMode = true;
//			return this;
//		}
//	}


    /************** 通用弹窗 AlertDialog************/
    @SuppressWarnings("deprecation")

    private Context context;
    private DialogInterface.OnClickListener confirmListener;
    private DialogInterface.OnClickListener cancelListener;
    private String title;
    private String hintMsg;
    private String confirmText;
    private String cancelText;
    private boolean isConfirmVisible = true;
    private boolean isCancelVisible = true;
    private boolean isTitleVisible = true;
    private boolean isInputMode = false;

    public BaseDialog(Context context) {
        this.context = context;
    }

    public static BaseDialog with(Context context) {
        return new BaseDialog(context);
    }


    private void initDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (isTitleVisible && !TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        } else {
            builder.setTitle(R.string.dialog_alert_title);
        }
        if (!TextUtils.isEmpty(hintMsg)) {
            builder.setMessage(hintMsg);
        }
        if (isConfirmVisible) {
            if (!TextUtils.isEmpty(confirmText)) {
                builder.setPositiveButton(confirmText, confirmListener);
            } else {
                builder.setPositiveButton(R.string.dialog_btn_confirm_text, confirmListener);
            }
        }
        if (isCancelVisible) {
            if (!TextUtils.isEmpty(confirmText)) {
                builder.setNegativeButton(cancelText, cancelListener);
            } else {
                builder.setNegativeButton(R.string.dialog_btn_cancel_text, cancelListener);
            }
        }
        builder.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                return true;
            }
            return false;
        });
        builder.setCancelable(false);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();


    }

    public void show() {
        initDialog();
    }

    public BaseDialog setHintMsg(String hintMsg) {
        this.hintMsg = hintMsg;
        return this;
    }

    public BaseDialog setHintMsg(int resId) {
        this.hintMsg = ResUtils.getString(resId);
        return this;
    }

    public BaseDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public BaseDialog setTitleVisible(boolean isTitleVisible) {
        this.isTitleVisible = isTitleVisible;
        return this;
    }

    public BaseDialog setConfirmClickListener(DialogInterface.OnClickListener confirmListener) {
        this.confirmListener = confirmListener;
        return this;
    }

    public BaseDialog setCancelListener(DialogInterface.OnClickListener cancelListener) {
        this.cancelListener = cancelListener;
        return this;
    }

    public BaseDialog setConfirmVisible(boolean confirmVisible) {
        this.isConfirmVisible = confirmVisible;
        return this;
    }

    public BaseDialog setCancelVisible(boolean cancelVisible) {
        this.isCancelVisible = cancelVisible;
        return this;
    }

    public BaseDialog setConfirmText(String confirmText) {
        this.confirmText = confirmText;
        return this;
    }

    public BaseDialog setCanceText(String cancelText) {
        this.cancelText = cancelText;
        return this;
    }

    public BaseDialog setInputMode() {
        this.isInputMode = true;
        return this;
    }


    /************** 便捷方法 ************/

    /**
     * 显示确认和取消对话框
     *
     * @param title           对话框标题
     * @param content         对话框内容
     * @param confirmListener 点击确认的监听器
     * @param cancelListener  点击取消的监听器
     */
//	private void showConfirm(FragmentManager fragmentManager,
//							String title,
//							String content,
//							View.OnClickListener confirmListener,
//							View.OnClickListener cancelListener) {
//		//通过BindViewHolder拿到控件对象,进行修改
//		new TDialog.Builder(fragmentManager)
//				.setLayoutRes(R.layout.dialog_custom_layout)    //设置弹窗展示的xml布局
//				.setScreenWidthAspect(AppUtil.getAppContext(), 0.8f)   //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
////                .setScreenHeightAspect(this, 1f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
//				.setGravity(Gravity.CENTER)     //设置弹窗展示位置
//				.setTag("dialog")   //设置Tag
//				.setDimAmount(0.6f)     //设置弹窗背景透明度(0-1f)
//				.setCancelableOutside(false)     //弹窗在界面外是否可以点击取消
////                    .setDialogAnimationRes(R.style.animate_dialog)  //设置弹窗动画
//				.setOnDismissListener(dialog -> {
//				})
//				.setOnBindViewListener(holder -> {
//					TextView tvTitle = holder.getView(R.id.tvTitle);
//					TextView tvHintMsg = holder.getView(R.id.tvHintMsg);
//					TextView tvConfirm = holder.getView(R.id.tvConfirm);
//					TextView tvCancel = holder.getView(R.id.tvCancel);
//					AppCompatEditText EtInput = holder.getView(R.id.EtInput);
//					if (!TextUtils.isEmpty(title)) {
//						tvTitle.setText(title);
//					} else {
//						tvTitle.setText("提示");
//					}
//					if (!TextUtils.isEmpty(content)) {
//						tvHintMsg.setText(content);
//					}
//				})
//				.addOnClickListener(R.id.tvCancel, R.id.tvConfirm)   //添加进行点击控件的id
//				.setOnViewClickListener((viewHolder, view, dialog) -> {
//					int i = view.getId();
//					if (i == R.id.tvCancel) {
//						if (cancelListener != null) {
//							cancelListener.onClick(view);
//						}
//						dialog.dismiss();
//					} else if (i == R.id.tvConfirm) {
//						if (confirmListener != null) {
//							confirmListener.onClick(view);
//						}
//						dialog.dismiss();
//					}
//				})
//				.create()   //创建TDialog
//				.show();    //展示
//	}
//
//	public void showConfirm(FragmentManager fragmentManager,
//							String content,
//							View.OnClickListener confirmListener) {
//		showConfirm(fragmentManager, null, content, confirmListener, null);
//	}
}
